package cn.tedu.tea.service;

import cn.tedu.tea.domain.UserAddress;
import cn.tedu.tea.domain.param.UserAddressAddNewDTO;
import cn.tedu.tea.domain.param.UserAddressUpdateDTO;
import cn.tedu.tea.domain.vo.UserAddressListVO;
import cn.tedu.tea.domain.vo.UserAddressStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.UserAddressMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserAddressServiceImpl implements IUserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public void addNew(UserAddressAddNewDTO userAddressAddNewDTO) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressAddNewDTO, userAddress);
        userAddress.setModifiedUserId(0).setModifiedUserName("");
        int rows = userAddressMapper.insert(userAddress);
        if (rows != 1){
            String message = "添加用户地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        UserAddressStandardVO userAddressStandard = userAddressMapper.getStandardById(id);
        if (userAddressStandard == null){
            String message = "删除用户地址失败，要删除的用户地址不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = userAddressMapper.deleteById(id);
        if (rows != 1){
            String message = "删除用户地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        int rows = userAddressMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除用户地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(UserAddressUpdateDTO userAddressUpdateDTO) {
        UserAddressStandardVO userAddressStandard = userAddressMapper.getStandardById(userAddressUpdateDTO.getId());
        if (userAddressStandard == null){
            String message = "修改用户地址失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressUpdateDTO, userAddress);
        int rows = userAddressMapper.update(userAddress);
        if (rows != 1){
            String message = "修改用户地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        updateStatusById(id, 1);
    }

    @Override
    public UserAddressStandardVO getStandardById(Integer id) {
        UserAddressStandardVO userAddressStandard = userAddressMapper.getStandardById(id);
        if (userAddressStandard == null){
            String message = "查询用户地址详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return userAddressStandard;
    }

    @Override
    public List<UserAddressListVO> list() {
        List<UserAddressListVO> list = userAddressMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        UserAddressStandardVO currentUserAddress = userAddressMapper.getStandardById(id);
        if (currentUserAddress == null) {
            String message = STATUS_TEXT[status] + "用户地址失败，用户地址数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentUserAddress.getStatus() == status) {
            String message = STATUS_TEXT[status] + "用户地址失败，此用户地址已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        UserAddress updateUserAddress = new UserAddress();
        updateUserAddress.setId(id);
        updateUserAddress.setStatus(status);
        int rows = userAddressMapper.update(updateUserAddress);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "用户地址失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
